<?php

session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}
ob_start();
if (isset($_REQUEST['timeout'])) {
    echo "<script>alert('로그아웃이 되었습니다');</script> ";
}

//echo $_SESSION['db'];
function curPageName() {
    return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}


$_SESSION['rows_page'] = 20;
$_SESSION['accrows_page'] = 5;
//$title = $_SESSION['browser'];
//$error = $_SESSION['error'];
$content = "content/Login_content.php";
$handler = "handle/loginHandler.php";
//echo $_SESSION['lang'];
$curPageName = curPageName();
$_SESSION['lastpage'] = "../login.php";

if (isset($_COOKIE['IDEmployee']) && $_COOKIE['IDEmployee'] != "") {
    //echo "cooke";
    $_SESSION['IDEmployee'] = $_COOKIE['IDEmployee'];
    include "operations/connection.php";

    $query = mysql_query("select IDEmployee,UserName,FullName,IDRole,CurrentLanguage,employee.Accountant from employee where IDEmployee='" . $_SESSION['IDEmployee'] . "' and State=1");
    while ($row = mysql_fetch_array($query)) {
        mysql_query("INSERT INTO checklogtime (IDEmployee, TimeLogIn) VALUES ('" . $row['IDEmployee'] . "', NOW())");
        $_SESSION['user'] = $row['UserName'];
        $_SESSION['fullname'] = $row['FullName'];
        $_SESSION['role'] = $row['IDRole'];
        $_SESSION['lang'] = $row['CurrentLanguage'];
        $_SESSION['ketoan'] = $row[Accountant];

        mysql_close($connection);

        $lang = $_SESSION['lang'];

        if ($lang == '') {
            $lang = 'EN';
            $_SESSION['lang'] = "EN";
        }

        if ($row[IDRole] == 2) {
            header('Location:home.php');
        } else if ($row[IDRole] == 1) {
            header('Location:homeAdmin.php');
        } else {
            header('Location:partnermanage.php');
        }
    }
} else if (isset($_SESSION['IDEmployee']) && $_SESSION['IDEmployee'] != "") {

    $lang = $_SESSION['lang'];
    if ($lang == '') {
        $lang = 'EN';
        $_SESSION['lang'] = "EN";
    }
    if ($_SESSION['role'] == 2)
        header('Location: home.php');
    if ($_SESSION['role'] == 1)
        header('Location: homeAdmin.php');
    if ($_SESSION['role'] == 3)
        header('Location: partnermanage.php');
}
require("LoginTemplate.php");
?>
<? ob_flush(); ?>